package com.fruit.service.promotion.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fruit.mapper.promotion.ActivityMapper;
import com.fruit.pojo.promotion.ActivityRule;
import com.fruit.service.promotion.ActivityRuleManager;
import com.fruit.util.ResultInfo;
import com.fruit.util.ResultUtil;

import lombok.extern.slf4j.Slf4j;

/**
 * 活动规则service
 *
 * @author Administrator
 * @version 2018.02.06 17:02
 *
 */
@Slf4j
@Service
public class ActivityRuleService implements ActivityRuleManager {
	@Autowired
	private ActivityMapper activityMapper;
	
	@Override
	public Map<String, Object> getActivityRules(Map<String, Object> params, String tradeId) {
		Map<String, Object> result = new HashMap<String, Object>();
		log.info("【" +tradeId+ "】---【获取活动规则】---【开始】---");
		try {
			// 校验参数
			if (params == null) {
				result.put("result", ResultUtil.initResult(ResultInfo.ERROR, "param_error", "参数错误!"));
				return result;
			}
			log.info("【" +tradeId+ "】---【套餐立即购买参数校验】-----【params】" + params.toString());
			if (params.get("activityId") == null || "".equals(params.get("activityId"))) {
				result.put("result", ResultUtil.initResult(ResultInfo.ERROR, "param_error", "参数活动id不能为空!"));
				return result;
			}
			if (params.get("goodsId") == null || "".equals(params.get("goodsId"))) {
				result.put("result", ResultUtil.initResult(ResultInfo.ERROR, "param_error", "参数活动商品id不能为空!"));
				return result;
			}
			List<ActivityRule> list = activityMapper.selectActivityRulesById(params);
			result.put("list", list);
			result.put("result", ResultUtil.initResult(ResultInfo.SUCCESS, "success", "操作成功"));
			log.info("【" +tradeId+ "】---【获取活动规则】---【成功】---");
		} catch (Exception e) {
			log.error("【" +tradeId+ "】---【获取活动规则】---【异常】---{}", e);
			result.put("result", ResultUtil.initResult(ResultInfo.ERROR, "exception", "系统异常!"));
		}
		log.info("【" +tradeId+ "】---【获取活动规则】---【结束】---");
		return result;
	}

	@Override
	public Map<String, Object> getUserChildsNum(Map<String, Object> params, String tradeId) {
		Map<String, Object> result = new HashMap<String, Object>();
		log.info("【" +tradeId+ "】---【获取活动用户下级数量】---【开始】---");
		try {
			// 校验参数
			if (params == null) {
				result.put("result", ResultUtil.initResult(ResultInfo.ERROR, "param_error", "参数错误!"));
				return result;
			}
			log.info("【" +tradeId+ "】---【获取活动用户下级数量参数校验】-----【params】" + params.toString());
			if (params.get("activityId") == null || "".equals(params.get("activityId"))) {
				result.put("result", ResultUtil.initResult(ResultInfo.ERROR, "param_error", "参数活动id不能为空!"));
				return result;
			}
			if (params.get("userId") == null || "".equals(params.get("userId"))) {
				result.put("result", ResultUtil.initResult(ResultInfo.ERROR, "param_error", "用户id不能为空!"));
				return result;
			}
			//直接关系
			params.put("type", "00");
			int num = activityMapper.countUserChildNum(params);
			result.put("num", num);
			result.put("result", ResultUtil.initResult(ResultInfo.SUCCESS, "success", "操作成功"));
			log.info("【" +tradeId+ "】---【获取活动用户下级数量】---【成功】---num={}", num);
		} catch (Exception e) {
			log.error("【" +tradeId+ "】---【获取活动用户下级数量】---【异常】---{}", e);
			result.put("result", ResultUtil.initResult(ResultInfo.ERROR, "exception", "系统异常!"));
		}
		log.info("【" +tradeId+ "】---【获取活动用户下级数量】---【结束】---");
		return result;
	}
	
}
